<?php
/*
 * @name 订单逻辑处理处理类
 * @author Jin
 * @date 2017.12.25
 **/
namespace app\admin\controller;

use app\common\model\Orders;
use think\facade\Request;
use PHPExcel_IOFactory;
use PHPExcel;

class Order extends Base
{
    /*
     * @name 展示订单
     * @return order.html
     **/
    public function orderList()
    {
        $where = [];
        if (!empty(Request::post('add_time_begin'))) {
            $where[] = ['ord_create_time', '>', strtotime(Request::post('add_time_begin'))];
        }
        if (!empty(Request::post('add_time_end'))) {
            $where[] = ['ord_create_time', '<', strtotime(Request::post('add_time_end'))];
        }
        if (!empty(Request::post('ord_num'))) {
            $where[] = ['ord_num', 'like', '%' . Request::post('ord_num') . '%'];
        }
        if (!empty(Request::post('ord_status'))) {
            $where[] = ['ord_status', '=', Request::post('ord_status')];
        }
        //获取所有订单
        $orderlist = Orders::where($where)->order('ord_create_time','desc')->paginate(5);
        $ordercount = Orders::count();
        //将用户id改为姓名,商户id改为姓名
        $agent = new \app\common\model\User();
        $shop = new \app\common\model\Shops();
        foreach ($orderlist as $k => $v) {
            $orderlist[$k]['ord_user_id'] = $agent->getNameByUserId($v['ord_user_id']);
            $orderlist[$k]['ord_shop_id'] = $shop->getNameByShopId($v['ord_shop_id']);
        }

        $this->assign('ordercount', $ordercount);
        $this->assign('orderlist', $orderlist);
        return $this->fetch('order');
    }
    /**
     * [dowload_excel 订单导出]
     * @return [type] [description]
     */
    public function dowload_excel(){
        $path = dirname(__FILE__); //找到当前脚本所在路径
        $PHPExcel = new PHPExcel(); //实例化PHPExcel类，类似于在桌面上新建一个Excel表格
        $PHPSheet = $PHPExcel->getActiveSheet(); //获得当前活动sheet的操作对象
        $PHPSheet->setTitle('订单资料'); //给当前活动sheet设置名称
        $PHPSheet->setCellValue('A1','订单ID')
          ->setCellValue('B1','订单编号')
          ->setCellValue('C1','用户')
          ->setCellValue('D1','商户')
          ->setCellValue('E1','支付状态')
          ->setCellValue('F1','总金额')
          ->setCellValue('G1','实付金额')
          ->setCellValue('H1','优惠金额')
          ->setCellValue('I1','优惠券ID')
          ->setCellValue('J1','下单时间');
        $PHPSheet->getColumnDimension('A')->setWidth(20);
        $PHPSheet->getColumnDimension('B')->setWidth(20);
        $PHPSheet->getColumnDimension('D')->setWidth(15);
        $PHPSheet->getColumnDimension('E')->setWidth(20);
        $query = Orders::select();
        foreach($query as $k=>$v){
          $PHPSheet->setCellValue('A'.($k+2),$v['ord_id'])
              ->setCellValue('B'.($k+2),$v['ord_num'])
              ->setCellValue('C'.($k+2),$v['ord_user_id'])
              ->setCellValue('D'.($k+2),$v['ord_shop_id'])
              ->setCellValue('E'.($k+2),$v['ord_status'])
              ->setCellValue('F'.($k+2),$v['ord_total_price'])
              ->setCellValue('G'.($k+2),$v['ord_real_price'])
              ->setCellValue('H'.($k+2),$v['ord_cou_price'])
              ->setCellValue('I'.($k+2),$v['ord_cou_id'])
              ->setCellValue('J'.($k+2),$v['ord_create_time']);
        }
        $PHPWriter = PHPExcel_IOFactory::createWriter($PHPExcel,'Excel2007');//按照指定格式生成Excel文件，‘Excel2007’表示生成2007版本的xlsx，‘Excel5’表示生成2003版本Excel文件
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//告诉浏览器输出07Excel文件
        //header('Content-Type:application/vnd.ms-excel');//告诉浏览器将要输出Excel03版本文件
        header('Content-Disposition: attachment;filename="01simple.xlsx"');//告诉浏览器输出浏览器名称
        header('Cache-Control: max-age=0');//禁止缓存
        $PHPWriter->save("php://output");
    }
}
